unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Timer1: TTimer;
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    procedure draw;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
const
  N=5;
  R=20;
  T=50;
  L=50;
  P=20;

var
  i, j, k, o: integer;
  arr: array of integer;
procedure TForm1.draw;
begin
  for k:=0 to N-i-1 do
  begin
    image1.Canvas.Brush.Color:=1000*arr[k];
    image1.Canvas.Ellipse(L+P*i+2*R*i, T+P*k+2*R*k, L+P*i+2*R*(i+1), T+P*k+2*R*(k+1));
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  setlength(arr, N);
  for j:=0 to N-1 do
    arr[j]:=random(100);
  j:=0;
  k:=0;
  i:=0;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (arr[j]>=arr[j+1]) then
  begin
    o:=arr[j];
    arr[j]:=arr[j+1];
    arr[j+1]:=o;
  end;
  j:=j+1;
  if (j=N-1) then
  begin
    draw;
    j:=0;
    i:=i+1;
  end;
  if (i=N) then
  begin
    timer1.Enabled:=false;
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  i:=0;
  for j:=0 to N-1 do
    arr[j]:=random(1000);
  j:=0;
  k:=0;
  image1.Canvas.Brush.Color:=clWhite;
  image1.Canvas.rectangle(clientrect);
  timer1.Enabled:=true;
end;

end.

